﻿package com.vicentediaz.view.imageviewer
{
    import com.vicentediaz.view.menu.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import org.papervision3d.core.geom.renderables.*;
    import org.papervision3d.materials.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.view.*;

    public class ZDepthImage extends BasicView
    {
        private var started:Boolean = false;
        public var planeXPixels:uint = 70;
        public var planeYPixels:uint = 70;
        private var angle:TextField;
        private var cameraZ:Number = 0;
        private var imageMaterial:BitmapMaterial;
        private var imageContainer:DisplayObject3D;
        private var _orbitate:Boolean = false;
        private var cameraAngleX:Number = 0;
        private var cameraAngleY:Number = 0;
        private var mesh:Plane;
        public static const verbose:Boolean = true;
        private static var instance:ZDepthImage;

        public function ZDepthImage(param1:Number = 640, param2:Number = 480, param3:Boolean = true, param4:Boolean = false, param5:String = "Target")
        {
            super(param1, param2, param3, param4, param5);
            this.say("Inicializado");
            return;
        }// end function

        public function set orbitate(param1:Boolean) : void
        {
            this._orbitate = param1;
            if (!ToolButtons.enabled3D)
            {
                this._orbitate = false;
            }// end if
            return;
        }// end function

        private function onImageEnterFrame(param1:Event) : void
        {
            this.render();
            return;
        }// end function

        private function render(param1:Boolean = false) : void
        {
            var _loc_4:Number;
            var _loc_5:Number;
            var _loc_6:Number;
            var _loc_7:Number;
            var _loc_2:Number;
            var _loc_3:Number;
            if (this._orbitate)
            {
                _loc_4 = stage.stageWidth / 2;
                _loc_5 = stage.stageHeight / 2;
                _loc_6 = mouseX - _loc_4;
                _loc_7 = mouseY - _loc_5;
                this.cameraAngleY = _loc_6 / _loc_4 * _loc_3;
                this.cameraAngleX = _loc_7 / _loc_5 * _loc_2;
                this.cameraZ = camera.zoom * camera.focus - Math.abs(camera.z);
                this.imageMaterial.smooth = false;
                cacheAsBitmap = false;
            }
            else
            {
                this.cameraZ = camera.zoom * camera.focus - Math.abs(camera.z);
                this.cameraAngleX = 0;
                this.cameraAngleY = 0;
                this.imageMaterial.smooth = true;
                cacheAsBitmap = true;
            }// end else if
            this.imageContainer.rotationY = this.imageContainer.rotationY - (this.imageContainer.rotationY - this.cameraAngleY) * 0.1;
            this.imageContainer.rotationX = this.imageContainer.rotationX - (this.imageContainer.rotationX - this.cameraAngleX) * 0.1;
            if (Math.abs(this.imageContainer.rotationY - this.cameraAngleY) < 0.001)
            {
                this.imageContainer.rotationY = this.cameraAngleY;
            }// end if
            if (Math.abs(this.imageContainer.rotationX - this.cameraAngleX) < 0.001)
            {
                this.imageContainer.rotationX = this.cameraAngleX;
            }// end if
            if (this.imageContainer.rotationY != this.cameraAngleY && this.imageContainer.rotationX != this.cameraAngleX || param1)
            {
                singleRender();
            }// end if
            return;
        }// end function

        private function greyLevel(param1:uint) : uint
        {
            var _loc_2:* = param1;
            var _loc_3:* = _loc_2 >> 16 & 255;
            var _loc_4:* = _loc_2 >> 8 & 255;
            var _loc_5:* = _loc_2 & 255;
            return _loc_3;
        }// end function

        public function build(param1:BitmapData, param2:BitmapData) : void
        {
            var _loc_12:Vertex3D;
            var _loc_13:Number;
            var _loc_14:Number;
            var _loc_15:Number;
            var _loc_16:Number;
            var _loc_17:Number;
            var _loc_18:Number;
            var _loc_19:Number;
            this.say("Creando nueva im?gen");
            var _loc_3:* = param2.width * 0.5;
            var _loc_4:* = param2.height * 0.5;
            var _loc_5:* = int(param1.width / this.planeXPixels);
            var _loc_6:* = int(param1.height / this.planeYPixels);
            var _loc_7:* = param1.width / param2.width;
            var _loc_8:* = param1.height / param2.height;
            var _loc_9:uint;
            var _loc_10:Number;
            var _loc_11:Number;
            if (!this.imageContainer)
            {
                this.imageContainer = new DisplayObject3D();
            }// end if
            if (this.imageMaterial)
            {
                this.imageMaterial.destroy();
                this.imageMaterial = null;
            }// end if
            if (this.mesh)
            {
                this.imageContainer.removeChild(this.mesh);
                this.mesh = null;
            }// end if
            this.imageMaterial = new BitmapMaterial(param2, false);
            this.mesh = new Plane(this.imageMaterial, param2.width, param2.height, _loc_5, _loc_6);
            var _loc_20:uint;
            while (_loc_20++ < this.mesh.geometry.vertices.length)
            {
                // label
                _loc_12 = Vertex3D(this.mesh.geometry.vertices[_loc_20]);
                _loc_14 = (_loc_12.x + _loc_3) * _loc_7;
                _loc_15 = (param2.height - (_loc_12.y + _loc_4)) * _loc_8;
                _loc_18 = this.greyLevel(param1.getPixel(Math.abs(_loc_14--), Math.abs(_loc_15--)));
                _loc_12.z = (-_loc_18) * 2;
                _loc_13 = Math.sqrt(_loc_12.x * _loc_12.x + _loc_12.y * _loc_12.y);
                _loc_16 = _loc_12.x / _loc_13;
                _loc_17 = _loc_12.y / _loc_13;
                _loc_19 = _loc_10 * _loc_12.z + _loc_11;
                _loc_13 = _loc_13 * (_loc_19 * 0.01);
                _loc_12.x = _loc_16 * _loc_13;
                _loc_12.y = _loc_17 * _loc_13;
            }// end while
            this.imageContainer.addChild(this.mesh);
            if (!contains(viewport))
            {
                addChild(viewport);
            }// end if
            camera.z = -1000;
            camera.zoom = 100;
            if (!this.started)
            {
                scene.addChild(this.imageContainer);
                addEventListener(Event.ENTER_FRAME, this.onImageEnterFrame, false, 0, true);
                addEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage, false, 0, true);
            }// end if
            this.render(true);
            this.started = true;
            return;
        }// end function

        private function say(param1:String) : void
        {
            if (verbose)
            {
                trace("<< ZDEPTH IMAGE : " + param1 + " >>");
            }// end if
            return;
        }// end function

        private function onRemovedFromStage(param1:Event) : void
        {
            if (this.imageMaterial)
            {
                this.imageMaterial.destroy();
            }// end if
            if (this.mesh)
            {
                this.mesh = null;
            }// end if
            removeEventListener(Event.ENTER_FRAME, this.onImageEnterFrame);
            return;
        }// end function

        public static function getInstance() : ZDepthImage
        {
            instance = instance ? (instance) : (new ZDepthImage(640, 480, false));
            return instance;
        }// end function

    }
}
